package com.vil.controller;

import com.vil.common.Constants;
import com.vil.common.ServerResponse;
import com.vil.entity.Admin;
import com.vil.entity.Charge;
import com.vil.entity.Detail;
import com.vil.service.ChargeService;
import com.vil.service.DetailService;
import com.vil.service.VillageService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/charge")
public class ChargeController extends BaseController{

    @Autowired
    private ChargeService chargeService;

    @Autowired
    private DetailService detailService;

    @Autowired
    private VillageService villageService;

    /**
     * 收费项目管理
     * @param model
     * @return
     */
    //去收费页面
    @RequestMapping("payService.html")
    public String payService(Model model, HttpSession session){
        Admin currentAdmin = (Admin)session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        ServerResponse serverResponse = chargeService.getListByManyConditions(1, 5, null);
        model.addAttribute("pro",serverResponse.getData());
        model.addAttribute("admin",currentAdmin);
        return "admin/pro_list";
    }
    //获取收费项目列表
    @PostMapping("viewchargelist")
    @ResponseBody
    public ServerResponse viewequipmentlist(@RequestParam(value = "condition",defaultValue = "",required = false) String condition,
                                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                            @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                            Model model){
        Charge charge = new Charge();
        charge.setPorject_name(condition);
        return chargeService.getListByManyConditions(pageIndex,pageSize,charge);

    }

    //去新增页面
    @GetMapping("pro_add.html")
    public String device_add(Model model){
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/pro_add";
    }

    //新增收费项目
    @PostMapping("addChargeSelective")
    public ServerResponse addChargeSelective(Charge charge){
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("您的账号未被激活，不能进行该操作");
        }
        return chargeService.insertSelective(charge);
    }

    //去编辑页面
    @GetMapping("pro_edit.html/{id}")
    public String device_edit(@PathVariable Long id,Model model){
        ServerResponse byId = chargeService.getById(id);
        model.addAttribute("e",byId.getData());
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/pro_edit";
    }

    //修改
    @PostMapping("editCharge")
    @ResponseBody
    public ServerResponse editDeviceSelective(Charge charge){

        return chargeService.updateByPrimaryKeySelective(charge);
    }

    @DeleteMapping("deleteChargeById/{id}")
    @ResponseBody
    public ServerResponse deleteChargeById(@PathVariable Long id){

        return  chargeService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteChargeByIds")
    @ResponseBody
    public ServerResponse deleteChargeByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return chargeService.deleteMany(array);
    }

    /**
     * 收费明细管理
     */

    //去收费页面
    @RequestMapping("chargeDetailsManagement.html")
    public String chargeDetailsManagement(Model model, HttpSession session){
        Admin currentAdmin = (Admin)session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        ServerResponse serverResponse = detailService.getListByManyConditions(1, 5, null);
        model.addAttribute("detail",serverResponse.getData());
        model.addAttribute("admin",currentAdmin);
        return "admin/promx_list";
    }
    //获取收费项目列表
    @PostMapping("viewdetaillist")
    @ResponseBody
    public ServerResponse viewdetaillist(@RequestParam(value = "condition",defaultValue = "",required = false) String condition,
                                         @RequestParam(value = "condition1",defaultValue = "",required = false) String condition1,
                                         @RequestParam(value = "condition2",defaultValue = "",required = false) String condition2,
                                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                            @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                            Model model){
        Detail detail = new Detail();
        detail.setOwner(condition);
        detail.setVillage(condition1);
        detail.setCharge_name(condition2);
        return detailService.getListByManyConditions(pageIndex,pageSize,detail);

    }

    //去新增页面
    @GetMapping("promx_add.html")
    public String promx_add(Model model){
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/promx_add";
    }

    //新增设备
    @PostMapping("addDetailSelective")
    public ServerResponse addDetailSelective(Detail detail){
        return detailService.insertSelective(detail);
    }

    //去编辑页面
    @GetMapping("promx_edit.html/{id}")
    public String promx_edit(@PathVariable Long id,Model model){
        ServerResponse byId = detailService.getById(id);
        model.addAttribute("e",byId.getData());
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/promx_edit";
    }

    //修改
    @PostMapping("editDetail")
    @ResponseBody
    public ServerResponse editDetailSelective(Detail detail){
        return detailService.updateByPrimaryKeySelective(detail);
    }

    @DeleteMapping("deleteDetailById/{id}")
    @ResponseBody
    public ServerResponse deleteDetailById(@PathVariable Long id){

        return  detailService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteDetailByIds")
    @ResponseBody
    public ServerResponse deleteDetailByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return detailService.deleteMany(array);
    }
}
